<?php

namespace app\app\controller;

use app\app\model\ProjectBannerModel;
use app\app\model\ProjectScheduleXlModel;
use app\app\model\SysDictDataModel;
use app\app\model\UserModel;
use app\BaseController;
use app\app\model\ProjectModel;
use app\app\model\ProjectRiskModel;
use app\app\model\ProjectTDModel;
use app\app\model\ProjectZrrModel;
use app\Request;


class Project extends BaseController
{
    public function info()
    {
        $id = input('id', '');
        $list = ProjectModel::where('id', $id)->select();
        $this->success('success', $list);
    }


    public function list()
    {
        $page_size = input('pageSize', '20');
        $city = input('city', '');
        $project_name = input('projectName', '');
        $page_size = input('pageSize', '20');

        $city_map = ProjectModel::where('id', '<>', '0')->where('city', '<>', '')->column('city');
        $build_num = 0;
        $total_num = 0;

        $map = [];
        if ($city) {
            $map[] = ['city', 'like', '%' . $city . '%'];
        }
        if ($project_name) {
            $map[] = ['project_name', 'like', '%' . $project_name . '%'];
        }

        if (count($map)) {
            $list = ProjectModel::where($map)->paginate($page_size);
            $build_num = ProjectModel::where('id', '<>', '0')
                ->where('is_parent', 0)
                ->where('project_period', '=', '2')
                ->where($map)
                ->count();
            $total_num = ProjectModel::where('id', '<>', '0')
                ->where('is_parent', 0)
                ->where('kg_time_real', '<>', '')
                ->where($map)
                ->select()->count();
        } else {
            $build_num = ProjectModel::where('id', '<>', '0')
                ->where('is_parent', 0)
                ->where('project_period', '=', '2')
                ->count();
            $total_num = ProjectModel::where('id', '<>', '0')
                ->where('is_parent', 0)
                ->where('kg_time_real', '<>', '')
                ->select()->count();
            $list = ProjectModel::paginate($page_size);
        }

        $res['data'] = $list;
        $res['city'] = array_count_values($city_map);
        $res['build_num'] = $build_num;
        $res['total_num'] = $total_num;

        $this->success('success', $res);
    }

    public function city()
    {
        $data = $this->request->param();
//        $where = [];
        $where[] = ['is_parent', '=', 0];

        $type = input('type', '1');
        if ($type) {
            $start_time = date('Y-01-01 00:00:00', time());
            $end_time = date('Y-12-31 23:59:59', time());
            $where[] = ['tc_time', '>=', $start_time];
            $where[] = ['kg_time', '<=', $end_time];
        }

        //项目类型查询
        if (!empty($data['project_type'])) $where[] = ['project_type', '=', $data['project_type']];

        //规模查询
        if (!empty($data['level'])) $where[] = ['project_dy_level', '=', $data['level']];

        //项目名称
        if (isset($data['project_name'])) $where[] = ['project_name', 'LIKE', '%' . $data['project_name'] . '%'];

        //建设程度
//        if (!empty($data['project_period'])) $where[] = ['project_period', '=', $data['project_period']];

        if (!empty($data['project_period'])) {
            if ($data['project_period'] == 2) {
                $where[] = ['project_period', 'in', '2,4,1'];
            } else {
                $where[] = ['project_period', '=', $data['project_period']];
            }
        }

        $city_map = ProjectModel::where('id', '<>', '0')->where('city', '<>', '')->where($where)->column('city');
        $res = array_count_values($city_map);
        $this->success('success', $res);
    }

    public function analysis()
    {
        //在建数量  project_period  =2

        $start_time = date('Y-01-01 00:00:00', time());
        $end_time = date('Y-12-31 23:59:59', time());
        $where_project[] = ['tc_time', '>=', $start_time];
        $where_project[] = ['kg_time', '<=', $end_time];
        $where_project[] = ['is_parent', '=', 0];
        $where_build[] = ['project_period', 'in', '2,4,1'];
        $build_num = ProjectModel::where($where_project)->where($where_build)->count();

        //停工  project_period  =4
        $stop_num = ProjectModel::where($where_project)->where('project_period', 4)->count();

        //三级风险及以上
        $start_time = date('Y-m-d 00:00:00', time());
        $end_time = date('Y-m-d 23:59:59', time());

        $where_risk[] = ['start_time', '<=', $end_time];
        $where_risk[] = ['end_time', '>=', $start_time];

        $where_td[] = ['start_date', '<=', $end_time];
        $where_td[] = ['end_date', '>=', $start_time];


        $risk_num = ProjectRiskModel::where($where_risk)
            ->where('risk_level', '<=', 3)->count();
        //停电计划
        $td_num = ProjectTDModel::where($where_td)->count();

        //分包队伍
        $where[] = ['a.dw_type', 'in', ['劳务分包', '专业分包']];
        $where[] = ['b.project_period', 'in', '2,4,1'];
        $zrr_num = ProjectZrrModel::alias('a')
//            ->field('a.id,a.project_name,a.dw_name,a.dw_type,b.id,b.link_project_id,c.project_name as project_main_name,c.id as main_id,c.kg_time as kg_main_time,c.tc_time as tc_main_time')
            ->leftjoin('t_project b', 'a.project_id = b.id')
            ->leftjoin('t_project c', 'b.link_project_id = c.id')
            ->where($where)->group('a.dw_name')->count();

        //总人数 1127
        $res = array(
            'build_num' => $build_num,
            'stop_num' => $stop_num,
            'risk_num' => $risk_num,
            'td_num' => $td_num,
            'zrr_num' => $zrr_num,
            'people_num' => 4805,
        );
        $this->success('success', $res);
    }

    public function projects()
    {
        $project_name = input('project_name', '');
        $user_name = input('user_name', '');
        $start_time = date('Y-01-01 00:00:00', time());
        $end_time = date('Y-12-31 23:59:59', time());
        $where[] = ['tc_time', '>=', $start_time];
        $where[] = ['kg_time', '<=', $end_time];
        $where[] = ['is_parent', '=', '1'];

        //用户名查询
        if ($user_name) {
            $where[] = ['link_user_id', 'like', '%' . $user_name . '%'];
        }
        //项目名查询
        if ($project_name) {
            $where[] = ['project_name', 'like', '%' . $project_name . '%'];
        }
        $list = ProjectModel::field('id,project_name,project_no')->where($where)->select();
        $this->success('success', $list);
    }

    /**
     * 本年输变电+单体项目
     */
    public function projectName()
    {
        $project_name = input('project_name', '');
        $link_user = input('link_user', '');
        $type = input('type', '');

        $start_time = date('Y-01-01 00:00:00', time());
        $end_time = date('Y-12-31 23:59:59', time());
        $where[] = ['b.tc_time', '>=', $start_time];
        $where[] = ['b.kg_time', '<=', $end_time];

        //项目名查询
        if ($project_name) {
            $where[] = ['b.project_name|a.project_name', 'like', '%' . $project_name . '%'];
        }
        if ($type == '项目经理') {
            $where[] = ['a.link_user_id', 'like', '%' . $link_user . '%'];
        }

        $list = ProjectModel::alias('a')
            ->field('a.*,b.project_name as project_main_name,b.id as main_id')
            ->join('t_project b', 'a.link_project_id = b.id')
            ->where($where)->select()->toArray();
        $info = $list;

        $tmp_arr = array();
        $temp = array();
        foreach ($info as $k => $v) {
            if (!in_array($v['main_id'], $tmp_arr)) {
                $tmp_arr[$k] = $v['main_id'];
                $temp[$k]['id'] = $v['main_id'];
                $temp[$k]['project_name'] = $v['project_main_name'];
            }
        }
        foreach ($temp as $key => $value) {
            foreach ($info as $k => $v) {
                if ($value['id'] == $v['link_project_id']) {
                    $temp[$key]['children'][] = $v;
                }
            }

        }
        $this->success('success', $temp);
    }


    public function charts()
    {

        $year = date('Y', time());
        $type = input('type', '1');
        //按月筛选
        $start_time = $year . '-01-01 00:00:00';
        $end_time = date('Y-m-d 23:59:59', time());

        $where = "is_parent = 0";
        $kg_time_real_where = "kg_time_real BETWEEN '" . $start_time . "' AND '" . $end_time . "'";
        $kg_time_where = "kg_time LIKE '%" . $year . "%'";

        $tc_time_real_where = "tc_time >= '" . $start_time . "'  AND tc_time_real <= '" . $end_time . "'";
        $tc_time_where = "tc_time LIKE '%" . $year . "%'";
        if ($type == 1) {
            $kg_num = ProjectModel::where($where)->where($kg_time_where)->count();
            $real_kg_num = ProjectModel::where($where)->where($kg_time_real_where)->count();
            $tc_num = ProjectModel::where($where)->where($tc_time_where)->count();
            $real_tc_num = ProjectModel::where($where)->where($tc_time_real_where)->count();
        } elseif ($type == 2) {
            $kg_num = ProjectModel::where($where)->where('kg_time', 'like', '%' . $year . '%')->SUM('project_scale_1');
            $real_kg_num = ProjectModel::where($where)->where($kg_time_real_where)->SUM('project_scale_1');
            $tc_num = ProjectModel::where($where)->where($tc_time_where)->SUM('project_scale_1');
            $real_tc_num = ProjectModel::where($where)->where($tc_time_real_where)->SUM('project_scale_1');
        } else {
            $kg_num = ProjectModel::where($where)->where($kg_time_where)->SUM('project_scale_2');
            $real_kg_num = ProjectModel::where($where)->where($kg_time_real_where)->SUM('project_scale_2');
            $tc_num = ProjectModel::where($where)->where($tc_time_where)->SUM('project_scale_2');
            $real_tc_num = ProjectModel::where($where)->where($tc_time_real_where)->SUM('project_scale_2');

            $kg_num = sprintf("%.2f", $kg_num);
            $real_kg_num = sprintf("%.2f", $real_kg_num);
            $tc_num = sprintf("%.2f", $tc_num);
            $real_tc_num = sprintf("%.2f", $real_tc_num);
        }


        $res = array(
//            'kg_num' => $kg_num,
//            'real_kg_num' => $real_kg_num,
//            'tc_num' => $tc_num,
//            'real_tc_num' => $real_tc_num,

            'tc_num' => $kg_num,
            'real_tc_num' => $real_kg_num,
            'kg_num' => $tc_num,
            'real_kg_num' => $real_tc_num,
        );
        $this->success('success', $res);
    }


    /**
     * 项目清单统计
     * @param Request $request
     * @throws \think\db\exception\DbException
     */
    public function project_list(Request $request)
    {
        $data = $request->param();
        $page_size = input('pageSize', '20');
        $where = '1=1';
        if ($data['project_period']) $where .= ' AND project_period = ' . $data['project_period'];
        if (isset($data['city'])) $where .= " AND city = '" . $data['city'] . "'";

        if ($data['type'] == 1) {
            //按年筛选
            $start_time = $data['year'] . '-01-01 00:00:00';
            $end_time = $data['year'] . '-12-31 23:59:59';
        } else {
            if (!isset($data['month'])) {
                $this->error('请输入月份');
            }
            //按月筛选
            $start_time = $data['year'] . '-' . $data['month'] . '-01 00:00:00';
            $end_time = date('Y-m-d 23:59:59', strtotime("$start_time +1 month -1 day"));
        }
        $where .= " AND over_time BETWEEN '" . $start_time . "' AND  '" . $end_time . "'";
        $info = ProjectModel::where($where)->field("COUNT(id) as total,city")->group('city')->select();
        $total = 0;
        foreach ($info as $key => $value) {
            $total += $value['total'];
            if ($value['city'] == NULL) {
                $value['city'] = '其他';
            }
        }
        $where .= " and is_parent =0";
        $list = ProjectModel::where($where)->paginate($page_size);
        $res = array(
            'total' => $total,
            'info' => $info,
            'list' => $list
        );
        $this->success('success', $res);
    }


    /**
     * 总体情况统计(工程总体情况)
     * @throws \think\db\exception\DbException
     */
    public function project_all()
    {
        $data = $this->request->param();
        $page_size = 50;
        $where[] = ['a.is_parent', '=', 0];

        //城市查询
        if (isset($data['city']) && !empty($data['city'])) {
            $city_arr = explode(',', $data['city']);
            $where[] = ['a.city', 'IN', $city_arr];
        }


        $type = input('type', '1');
//        if ($type) {
        $start_time = date('Y-01-01 00:00:00', time());
        $end_time = date('Y-12-31 23:59:59', time());
        $where[] = ['tc_time', '>=', $start_time];
        $where[] = ['kg_time', '<=', $end_time];
//        }


        //项目类型查询
        if (!empty($data['project_type'])) $where[] = ['a.project_type', '=', $data['project_type']];

        //规模查询
        if (!empty($data['level'])) $where[] = ['a.project_dy_level', '=', $data['level']];

        //项目名称
        if (isset($data['project_name'])) $where[] = ['a.project_name', 'LIKE', '%' . $data['project_name'] . '%'];

        //建设程度
        if (!empty($data['project_period'])) {
            if ($data['project_period'] == 2) {
                $where[] = ['a.project_period', 'in', '2,4,1'];
            } else {
                $where[] = ['a.project_period', '=', $data['project_period']];
            }
        }

        $list = ProjectModel::alias('a')->where($where)->order('')->paginate($page_size)->toArray();

        usort($list['data'], array($this, "compare"));


        $this->success('success', $list);


    }

    /**
     *  获取主体项目下的单体项目
     */
    public function project_son()
    {
        $project_id = input('project_id');
        $type = input('type', '');
        $where[] = ['link_project_id', '=', $project_id];
        if ($type) {
            $where[] = ['project_dxgc_type', '=', 3];
        }

        $list = ProjectModel::where($where)->select()->toArray();

        foreach ($list as $key => $value) {
            if (in_array($value['project_period'], [1, 2, 4])) {
                $list[$key]['project_period'] = '在建';
            } elseif ($value['project_period'] == 3) {
                $list[$key]['project_period'] = '投产';
            } else {
                $list[$key]['project_period'] = '停工';
            }
        }

        foreach ($list as $key => $value) {
            if ($value['link_user_id']) {
                $user_arr = explode(',', $value['link_user_id']);
                $where = [];
                $where[] = ['user_name', '=', $user_arr[0]];
                $users = UserModel::field('nick_name')->where($where)->find();
                $list[$key]['user_name'] = $users['nick_name'];
//                $where[] = ['user_name', 'IN', $user_arr];
//                $users = UserModel::field('nick_name')->where($where)->select()->toArray();
//                if (count($users) > 0) {
//                    $list[$key]['user_name'] = implode(",", array_column($users, 'nick_name'));
//                }
            }
        }
        usort($list, array($this, "compare"));
        $this->success('success', $list);
    }

    public function compare($a, $b)
    {
        $arr2 = array(2, 4, 1, 5, 3);
        return (array_search($a['project_period'], $arr2) < array_search($b['project_period'], $arr2)) ? -1 : 1;
    }

//    public function project_all(Request $request)
//    {
//        $data = $request->param();
//        $page_size = input('pageSize', '20');
//
//        $where[] = ['a.is_parent', '=', 1 ];
//
//        //城市查询
//        if (isset($data['city']) && !empty($data['city'])) {
//            $city_arr = explode(',', $data['city']);
//            $where[] = ['a.city', 'IN', $city_arr];;
//        }
//
//        //项目类型查询
//        if (!empty($data['project_type'])) $where[] = ['a.project_type', '=', $data['project_type']];
//
//        //规模查询
//        if (!empty($data['level'])) $where[] = ['a.project_dy_level', '=', $data['level']];
//
//        //计划开工查询
//        if (!empty($data['work_start_time']) && !empty($data['work_end_time'])) {
//            $where[] = ['a.kg_time', 'between', [$data['work_start_time'], $data['work_end_time']]];
//
//        } else {
//            if (!empty($data['work_start_time'])) $where[] = ['a.kg_time', '>', $data['work_start_time']];
//            if (!empty($data['work_end_time'])) $where[] = ['a.kg_time', '<', $data['work_end_time']];
//        }
//
//        //计划投产查询
//        if (!empty($data['into_start_time']) && !empty($data['into_end_time'])) {
//            $where[] = ['a.tc_time', 'between', [$data['into_start_time'], $data['into_end_time']]];
//        } else {
//            if (!empty($data['into_start_time'])) $where[] = ['a.tc_time', '>', $data['into_start_time']];
//            if (!empty($data['into_end_time'])) $where[] = ['a.tc_time', '<', $data['into_end_time']];
//        }
//
//        //项目名称
//        if (isset($data['project_name'])) $where[] = ['a.project_name', 'LIKE', '%' . $data['project_name'] . '%'];
//
//        //建设程度
//        if (!empty($data['project_period'])) $where[] = ['a.project_period', '=', $data['project_period']];
//
//        $list = ProjectModel::alias('a')
//            ->field('a.*,b.project_period as project_main_period, b.project_name as project_main_name,b.id as main_id,b.kg_time as kg_main_time,b.tc_time as tc_main_time')
//            ->leftjoin('t_project b', 'a.link_project_id = b.id')
//            ->group('a.project_name')
//            ->where($where)
//            ->order('a.project_dy_level desc')->paginate($page_size)->toArray();
//
//
//        $info = $list['data'];
//
//        $tmp_arr = array();
//        $temp = array();
//        foreach ($info as $k => $v) {
//            if (!in_array($v['main_id'], $tmp_arr)) {
//                $tmp_arr[$k] = $v['main_id'];
//                $temp[$k]['id'] = $v['main_id'];
//                $temp[$k]['project_name'] = $v['project_main_name'];
//                $temp[$k]['kg_time'] = $v['kg_main_time'];
//                $temp[$k]['kg_time'] = $v['tc_main_time'];
//                $temp[$k]['project_period'] = $v['project_main_period'];
//            }
//        }
//        foreach ($temp as $key => $value) {
//            foreach ($info as $k => $v) {
//                if ($value['id'] == $v['link_project_id']) {
//                    $temp[$key]['children'][] = $v;
//                }
//            }
//
//        }
//        $newTemp = [];
//        foreach ($temp as $k => $v) {
//            $newTemp[$k] = $v;
//        }
//        $list['data'] = $newTemp;
//
//
//        $this->success('success', $list);
//    }


    /**
     * 总体情况统计(在建工程)
     * @throws \think\db\exception\DbException
     */
    public function project_build()
    {
        $type = input('type', 1);
        $page_size = input('pageSize', '20');
        if ($type == 1) {
            //按地区
            if (isset($data['city'])) $where = " AND city = '" . $data['city'] . "'";
            $info = ProjectModel::field("COUNT(id) as total,city as name")->group('city')->select();
            $list = ProjectModel::where('is_parent', 0)->order('city desc')->paginate($page_size);
            $total = 0;
            foreach ($info as $key => $value) {
                $total += $value['total'];
                if ($value['name'] == NULL) {
                    $value['name'] = '其他';
                }
            }
        } elseif ($type == 2) {
            //按类型
            $info = ProjectModel::field("COUNT(id) as total,project_dxgc_type as name")->group('project_dxgc_type')->select();
            $list = ProjectModel::where('is_parent', 0)->order('project_dxgc_type desc')->paginate($page_size);
            $total = 0;

            $type = SysDictDataModel::field('dict_label,dict_value')->where('dict_type', 'project_dxgc_type')->select()->toArray();
            foreach ($info as $key => $value) {
                $total += $value['total'];
                foreach ($type as $k => $v) {
                    if ($value['name'] == $v['dict_value']) {
                        $value['name'] = $v['dict_label'];
                    }
                    if ($value['name'] == NULL) {
                        $value['name'] = '其他';
                    }
                }

            }
        } else {
            $info = ProjectModel::field("COUNT(id) as total, project_dy_level as name")->group('project_dy_level')->select();
            $list = ProjectModel::where('is_parent', 0)->order('project_dy_level desc')->paginate($page_size);
            $total = 0;
            foreach ($info as $key => $value) {
                $total += $value['total'];
                if ($value['name'] == NULL) {
                    $value['name'] = '其他';
                }
            }

        }
        $res = array(
            'total' => $total,
            'info' => $info,
            'list' => $list
        );
        $this->success('success', $res);
    }


    public function td()
    {
        $start_date = input('startDate', '');
        $end_date = input('endDate', '');
        $city = input('city', '');
        $project_name = input('projectName', '');
        $page_size = input('pageSize', '20');

        $map = [];
        if ($start_date) {
            $map[] = ['a.start_date', '>=', $start_date];
        }
        if ($end_date) {
            $map[] = ['a.end_date', '<=', $end_date];
        }
        if ($project_name) {
            $map[] = ['a.project_name', 'like', '%' . $project_name . '%'];
        }

        if ($city) {
            $map[] = ['b.city', 'like', '%' . $city . '%'];
        }

        $list = ProjectTDModel::alias('a')->field('a.*, b.city')->join('t_project b', 'a.project_id = b.id')->where($map)->paginate($page_size);
        $this->success('success', $list);
    }


    public function projectTower()
    {
        $project_id = input('project_id', '');
        $list = ProjectScheduleXlModel::field('id,xl_mark')->where('project_id', $project_id)->select();
        $this->success('success', $list);
    }


    public function reportAnalysis()
    {
        $project_id = input('project_id', '');

        $tongJi = array(
            'base_info' => ['finish' => 8, 'build' => 2],
            'tower_info' => ['finish' => 8, 'build' => 2],
            'string_info' => ['finish' => 8, 'build' => 2]
        );
        $this->success('success', $tongJi);
    }

    public function bannerList()
    {
        $project_id = input('project_id', '');
        $list = ProjectBannerModel::alias('a')->field('a.*,b.file_name,b.file_path')
            ->leftjoin('sys_file_info b', 'b.file_id = a.file_id')
            ->where('project_id', $project_id)
            ->select();
        $this->success('success', $list);
    }

    /**
     * 获取项目启动时间
     */
    public function getProjectStart()
    {
        $project_id = input('project_id', '');
        if (!$project_id) $this->error('请传入id');
        $info = ProjectModel::field('id,kg_time_real')->where('id', $project_id)->find();
        $this->success('success', $info);

    }

    /**
     * 修改项目启动时间
     */
    public function updateProjectStart()
    {
        $data = $this->request->param();
        if (!$data['project_id']) $this->error('请传入id');
        $info = ProjectModel::where('id', $data['project_id'])->find();
        if (empty($info)) $this->error('项目不存在');
        $res = $info->save($data);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }

    }

    /**
     * 获取项目投产时间
     */
    public function getProjectUsed()
    {
        $project_id = input('project_id', '');
        if (!$project_id) $this->error('请传入id');
        $info = ProjectModel::field('id,tc_time_real')->where('id', $project_id)->find();
        $this->success('success', $info);
    }

    /**
     * 修改项目投产时间
     */
    public function updateProjectUsed()
    {
        $data = $this->request->param();
        if (!$data['project_id']) $this->error('请传入id');
        $info = ProjectModel::where('id', $data['project_id'])->find();
        if (empty($info)) $this->error('项目不存在');
        $res = $info->save($data);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }


}
